const app = getApp()
Page({
    data: {
        navData: [],
        catalogSelect: 0,
        navIndex: 0,
        searchtype: false,
        searchinput: '',
        tokenID: '',
        title: '',
        duration: 2000,
        maskShow: false,
        page: 1,
        datamsg: false,
        moment: [],
        historylist: [],
        seeklist: [],
        hotkeyword: []
    },
    onLoad: function(options) {
        var _this = this;
        _this.setData({
            tokenID: app.data.tokenID
        })
        _this.onReachBottom();
        var historylist = wx.getStorageSync('historylist');
        var arr = [];
        if (historylist != '') {
            for (var i in JSON.parse(wx.getStorageSync('historylist'))) {
                arr.push(JSON.parse(wx.getStorageSync('historylist'))[i])
            }
            _this.setData({
                historylist: arr
            })
        }
        wx.request({
            url: app.data.api + 'Curriculum/classification',
            method: "POST",
            data: {
                "token": _this.data.tokenID,
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                var arr = [],
                    list = res.data.data;
                for (var i = 0; i < list.length; i++) {
                    arr.push(list[i])
                }
                _this.setData({
                    navData: arr
                })
            }
        })
        wx.request({
            url: app.data.api + 'Curriculum/hotkeyword',
            method: "POST",
            data: {
                "token": _this.data.tokenID,
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
                var arr = [],
                    list = res.data.data;
                for (var i = 0; i < list.length; i++) {
                    arr.push(list[i])
                }
                _this.setData({
                    hotkeyword: arr
                })
            }
        })
    },
    onPullDownRefresh: function() {
        var _this = this;
        this.setData({
            page: 1
        })
        wx.showNavigationBarLoading();
        _this.gtemomentList("down")
    },
    onReachBottom: function() {
        var that = this;
        wx.showLoading({
            title: '玩命加载中',
        })
        that.gtemomentList("up")
    },
    gtemomentList: function(type) {
        var that = this;
        that.setData({
            tokenID: app.data.tokenID
        })
        wx.request({
            url: app.data.api + 'Curriculum/list',
            data: {
                "page": that.data.page,
                "token": that.data.tokenID,
                "name": that.data.searchinput,
                "classid": that.data.catalogSelect,
            },
            method: "POST",
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                if (res.data.status) {
                    if (type == 'up') {
                        var moment_list = that.data.moment;
                        for (var i = 0; i < res.data.data.list.length; i++) {
                            moment_list.push(res.data.data.list[i]);
                        }
                        if (res.data.data.list.length == 0) {
                            that.setData({
                                datamsg: true
                            })
                        }
                        that.setData({
                            page: that.data.page + 1,
                            moment: that.data.moment
                        })
                    } else {
                        var moment_list = [];
                        for (var i = 0; i < res.data.data.list.length; i++) {
                            moment_list.push(res.data.data.list[i]);
                        }
                        that.setData({
                            page: that.data.page + 1,
                            moment: moment_list
                        })
                        wx.stopPullDownRefresh();
                    }
                    wx.hideLoading();
                    wx.hideNavigationBarLoading();
                } else {
                    wx.hideLoading();
                    wx.showModal({
                        title: '提示',
                        content: res.data.msg,
                        showCancel: false,
                        confirmColor: '#04A9F4',
                        success: function(res) {
                            if (res.confirm) {
                                wx.redirectTo({
                                    url: '../login/login'
                                })
                            } else if (res.cancel) {
                                wx.redirectTo({
                                    url: '../login/login'
                                })
                            }
                        }
                    });
                }
            }
        })
    },
    showSearch: function() {
        var _this = this,
            maskShow = _this.data.maskShow;
        this.setData({
            maskShow: !maskShow
        })
    },
    tapactive: function(event) {
        var that = this;
        that.setData({
            catalogSelect: event.currentTarget.dataset.id,
            navIndex: event.currentTarget.dataset.index,
            page: 1
        })
        that.gtemomentList("down")
    },
    changeText: function(event) {
        var _this = this;
        if (event.detail.value.length > 0) {
            _this.setData({
                searchinput: event.detail.value
            })
        } else {
            _this.setData({
                searchtype: false,
                searchinput: ''
            })
        }
    },
    emptysearch: function() {
        var _this = this;
        _this.setData({
            searchinput: '',
            searchtype: false
        })
    },
    searchCourse: function() {
        var _this = this,
            arr = [];
        arr.push(_this.data.searchinput);
        arr = arr.concat(_this.data.historylist)
        var historyarr = [...new Set(arr)];
        if (_this.data.searchinput) {
            _this.setData({
                historylist: historyarr,
                searchtype: true,
                page: 1
            })
            _this.gtemomentList();
            wx.setStorageSync('historylist', JSON.stringify(_this.data.historylist));
        } else {
            wx.showModal({
                title: '提示',
                content: '请输入搜索关键词',
                showCancel: false,
                confirmColor: '#04A9F4',
                success: function(res) {
                    if (res.confirm) {
                    } else if (res.cancel) {
                    }
                }
            })
            return false;
        }
    },
    deleteHistory: function(event) {
        var _this = this,
            type = event.currentTarget.dataset.id;
        if (type == 'historylist') {
            _this.setData({
                historylist: [],
            })
            wx.removeStorageSync("historylist")
        } else {
            var arr = _this.data.historylist;
            arr.splice(type, 1);
            _this.setData({
                historylist: arr,
            })
            wx.setStorageSync('historylist', JSON.stringify(_this.data.historylist))
        }
    },
    jumpplay: function(event) {

    },
    pushinputval: function(event) {
        var _this = this;
        _this.setData({
            searchinput: event.target.dataset.text
        })
        _this.searchCourse();
    }
})